const Base = require('./base.js');
module.exports = class extends Base {
	constructor(ctx) {
		super(ctx);
		this.order = this.model('store/order');
		this.order_goods = this.model('store/order_goods');
		this.user = this.model('system/user');
		this.region = this.model('store/region');
	}
	async indexAction() {
		if(this.isGet) {
			return this.display();
		}

		let posts = this.post();

		let where = {};
		if(!think.isEmpty(posts.userid)) {
			where.user_id = posts.userid;
		}
		const data = await this.order.dataTable(posts, where);
		console.log(JSON.stringify(data))
		return this.json(data);
	}

	async detailAction() {
		if(this.isGet) {
			let gets = this.get();
			let order = await this.order.findOne(gets.id);

			order.goods = await this.order_goods.where({
				order_id: order.id
			}).select();
			order.user = await this.user.findOne(order.user_id);

			this.assign("order", order);

			return this.display();
		}
	}

	async editAction() {
		if(this.isGet) {
			let gets = this.get();
			let order = await this.order.findOne(gets.id);

			order.goods = await this.order_goods.where({
				order_id: order.id
			}).select();
			order.user = await this.user.findOne(order.user_id);
			let region = {};
			region.country = await this.region.where({
				pid: 0
			}).select();
			region.province = await this.region.where({
				pid: order.country_id
			}).select();
			region.city = await this.region.where({
				pid: order.province_id
			}).select();
			region.district = await this.region.where({
				pid: order.city_id
			}).select();

			this.assign("order", order);
			this.assign("region", region);

			return this.display();
		}
		let posts = this.post();
		await this.order.setOne(posts.id, {
			discount: parseFloat(posts.discount),
			admin_remark: posts.admin_remark,
			accept_name: posts.accept_name,
			mobile: posts.mobile,
			country_id: posts.country,
			province_id: posts.province,
			city_id: posts.city,
			district_id: posts.district,
			address: posts.address,
			zip: posts.zip,
			real_price: parseFloat(posts.real_price) + parseFloat(posts.discount)-parseFloat(posts.orgin_discount)

		});
		return this.success();

	}

	async printerAction() {
		if(this.isGet) {
			return this.display();
		}
	}

};